package com.cartel.genius.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cartel.genius.dto.CategoryTO;
import com.cartel.genius.dto.PlaceTO;
import com.cartel.genius.entities.CategoryEntity;
import com.cartel.genius.entities.PlaceEntity;

/**
 * Utility to transform/populate bean properties
 * 
 * @author Naresh Reddy
 */
public class BeanTransformer {
	
	private static final Logger logger = LoggerFactory.getLogger(BeanTransformer.class);
	
	/**
	 * Utility method to transform CategoryEntity to CategoryTO
	 * 
	 * @param categoryEntity
	 * @return CategoryTO
	 */
	public static CategoryTO transformCategory(CategoryEntity categoryEntity) {
		logger.debug("Entered [transformCategory] of [BeanTransformer]");
		CategoryTO categoryTO = new CategoryTO();
		categoryTO.setCategoryId(categoryEntity.getCategoryId());
		categoryTO.setCategoryName(categoryEntity.getCategoryName());
		
		Calendar createdDate = Calendar.getInstance();
		createdDate.setTimeInMillis(categoryEntity.getCreatedDate());
		categoryTO.setCreatedDate(createdDate);
		
		Calendar modifiedDate = Calendar.getInstance();
		modifiedDate.setTimeInMillis(categoryEntity.getModifiedDate());
		categoryTO.setModifiedDate(modifiedDate);
		
		CategoryEntity parentCategory = categoryEntity.getParentCategory();
		if (parentCategory != null) {
			categoryTO.setParentCategoryId(parentCategory.getCategoryId());
			categoryTO.setParentCategoryName(parentCategory.getCategoryName());
			
			Calendar parentCreatedDate = Calendar.getInstance();
			parentCreatedDate.setTimeInMillis(parentCategory.getCreatedDate());
			categoryTO.setParentCreatedDate(parentCreatedDate);
			
			Calendar parentModifiedDate = Calendar.getInstance();
			parentModifiedDate.setTimeInMillis(parentCategory.getModifiedDate());
			categoryTO.setParentModifiedDate(parentModifiedDate);
		}
		
		logger.debug("Exiting [transformCategory] of [BeanTransformer]");
		return categoryTO;
	}
	
	/**
	 * Utility method to transform CategoryTO to CategoryEntity
	 * 
	 * @param categoryTO
	 * @return CategoryEntity
	 */
	public static CategoryEntity transformCategory(CategoryTO categoryTO) {
		logger.debug("Entered [transformCategory] of [BeanTransformer]");
		CategoryEntity categoryEntity = new CategoryEntity();
		categoryEntity.setCategoryId(categoryTO.getCategoryId());
		categoryEntity.setCategoryName(categoryTO.getCategoryName());
		
		if (categoryTO.getCreatedDate() != null) {
			categoryEntity.setCreatedDate(categoryTO.getCreatedDate().getTimeInMillis());
		}
		
		if (categoryTO.getModifiedDate() != null) {
			categoryEntity.setModifiedDate(categoryTO.getModifiedDate().getTimeInMillis());
		}
		
		CategoryEntity parentCategory = new CategoryEntity();
		parentCategory.setCategoryId(categoryTO.getParentCategoryId());
		parentCategory.setCategoryName(categoryTO.getParentCategoryName());
		
		if (categoryTO.getParentCreatedDate() != null) {
			parentCategory.setCreatedDate(categoryTO.getParentCreatedDate().getTimeInMillis());
		}
		
		if (categoryTO.getParentModifiedDate() != null) {
			parentCategory.setModifiedDate(categoryTO.getParentModifiedDate().getTimeInMillis());
		}
		
		categoryEntity.setParentCategory(parentCategory);
		
		logger.debug("Exiting [transformCategory] of [BeanTransformer]");
		return categoryEntity;
	}
	
	/**
	 * Utility method to transform PlaceEntity to PlaceTO
	 * 
	 * @param placeEntity
	 * @return PlaceTO
	 */
	public static PlaceTO transformPlace(PlaceEntity placeEntity) {
		logger.debug("Entered [transformPlace] of [BeanTransformer]");
		PlaceTO placeTO = new PlaceTO();
		placeTO.setPlaceId(placeEntity.getPlaceId());
		placeTO.setPlaceName(placeEntity.getPlaceName());
		placeTO.setPlaceCode(placeEntity.getPlaceCode());
		
		Calendar createdDate = Calendar.getInstance();
		createdDate.setTimeInMillis(placeEntity.getCreatedDate());
		placeTO.setCreatedDate(createdDate);
		
		Calendar modifiedDate = Calendar.getInstance();
		modifiedDate.setTimeInMillis(placeEntity.getModifiedDate());
		placeTO.setModifiedDate(modifiedDate);
		
		CategoryEntity placeCategory = placeEntity.getCategory();
		
		if (placeCategory != null) {
			placeTO.setCategoryTO(transformCategory(placeCategory));
		}
		
		PlaceEntity parentPlace = placeEntity.getParent();		
		
		if (parentPlace != null) {
			placeTO.setParentPlaceId(parentPlace.getPlaceId());
			placeTO.setParentPlaceName(parentPlace.getPlaceName());
			placeTO.setParentPlaceCode(parentPlace.getPlaceCode());
			
			Calendar parentCreatedDate = Calendar.getInstance();
			parentCreatedDate.setTimeInMillis(parentPlace.getCreatedDate());
			placeTO.setParentCreatedDate(parentCreatedDate);
			
			Calendar parentModifiedDate = Calendar.getInstance();
			parentModifiedDate.setTimeInMillis(parentPlace.getModifiedDate());
			placeTO.setParentModifiedDate(parentModifiedDate);
			
			CategoryEntity parentCategory = parentPlace.getCategory();
			
			if (parentCategory != null) {
				placeTO.setParentCatogoryTO(transformCategory(parentCategory));
			}
		}		
		
		logger.debug("Exiting [transformPlace] of [BeanTransformer]");
		
		return placeTO;
	}
	
	/**
	 * Utility method to transform list of place entities to PlacTO's
	 * 
	 * @param placeEntities
	 * @return
	 */
	public static List<PlaceTO> transformPlace(List<PlaceEntity> placeEntities) {
		logger.debug("Entered [transformPlace] of [BeanTransformer]");
		
		List<PlaceTO> placeTOList = new ArrayList<PlaceTO>();
		for (PlaceEntity placeEntity : placeEntities) {
			placeTOList.add(BeanTransformer.transformPlace(placeEntity));
		}
		
		logger.debug("Exiting [transformPlace] of [BeanTransformer]");
		
		return placeTOList;
	}
	
	/**
	 * Utility method to transform PlaceTO to PlaceEntity
	 * 
	 * @param placeTO
	 * @return PlaceEntity
	 */
	public static PlaceEntity transformPlace(PlaceTO placeTO) {
		logger.debug("Entered [transformPlace] of [BeanTransformer]");
		PlaceEntity placeEntity = new PlaceEntity();
		placeEntity.setPlaceId(placeTO.getPlaceId());
		placeEntity.setPlaceName(placeTO.getPlaceName());
		placeEntity.setPlaceCode(placeTO.getPlaceCode());
		
		if (placeTO.getCreatedDate() != null) {
			placeEntity.setCreatedDate(placeTO.getCreatedDate().getTimeInMillis());
		}
		
		if (placeTO.getModifiedDate() != null) {
			placeEntity.setModifiedDate(placeTO.getModifiedDate().getTimeInMillis());
		}
		
		CategoryTO categoryTO = placeTO.getCategoryTO();
		if (categoryTO != null) {
			placeEntity.setCategory(transformCategory(categoryTO));
		}		
		
		/*PlaceEntity parentPlace = new PlaceEntity();
		parentPlace.setPlaceId(placeTO.getParentPlaceId());
		parentPlace.setPlaceName(placeTO.getParentPlaceName());
		parentPlace.setPlaceCode(placeTO.getParentPlaceCode());
		
		if (placeTO.getParentCreatedDate() != null) {
			parentPlace.setCreatedDate(placeTO.getParentCreatedDate().getTimeInMillis());
		}
		
		if (placeTO.getParentModifiedDate() != null) {
			parentPlace.setModifiedDate(placeTO.getParentModifiedDate().getTimeInMillis());
		}
		
		placeEntity.setParent(parentPlace);*/
		logger.debug("Exiting [transformPlace] of [BeanTransformer]");
		
		return placeEntity;
	}

}
